package com.ruoyi.plan.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.plan.domain.PlanSubject;
import com.ruoyi.plan.domain.ReviewPlan;
import com.ruoyi.plan.domain.StudyContent;
import com.ruoyi.plan.service.IPlanSubjectService;
import com.ruoyi.plan.service.IReviewPlanService;
import com.ruoyi.plan.service.IStudyContentService;
import com.ruoyi.plan.service.IWorkSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author MLYNDHG
 * @version 1.0
 * @description TODO
 * @date 2023/12/7 17:13
 */
@Service
public class WorkSpaceServiceImpl  implements IWorkSpaceService
{

    @Autowired
    private IPlanSubjectService planSubjectService;
    @Autowired
    private IReviewPlanService reviewPlanService;
    @Autowired
    private IStudyContentService studyContentService;

    @Override
    public List<StudyContent> selectWorkSpaceTodyTodoList() {
        //1.查询科目开启的、uid为本人的 list
        PlanSubject planSubject = new PlanSubject();
        planSubject.setUid(SecurityUtils.getUserId());
        planSubject.setIsQueue(0L);
        List<PlanSubject> planSubjectList = planSubjectService.selectPlanSubjectList(planSubject);
        //2.通过1中查询的list的外键查询对应的学习内容表 使用1中的rankage markStatus isQueue  来筛选出今日学习内容list
        List<StudyContent> studyContentList = new ArrayList<>();
        for (int i = 0; i < planSubjectList.size(); i++) {
            StudyContent studyContent = new StudyContent();
            studyContent.setSubjectId(planSubjectList.get(i).getId());
            studyContent.setMarkStatus("0");
            studyContent.setIsQueue(0L);
            List<StudyContent> studyContents = studyContentService.selectStudyContentList(studyContent);
            //使用1中的rankage确定强度
            studyContentList.addAll(studyContents.stream().limit(planSubjectList.get(i).getRankAge()).collect(Collectors.toList()));
        }
        return studyContentList;
    }
    @Override
    public List<HashMap> selectWorkSpaceTodyReviewList() {
        //1.查询uid为本人、计划复习日期为今日的 review_plan==list
        ReviewPlan reviewPlan = new ReviewPlan();
        reviewPlan.setUid(SecurityUtils.getUserId());
        Date date = new Date();
        Date todyDate = new Date(date.getTime());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = dateFormat.format(todyDate);
        reviewPlan.setReviewDate(formattedDate);
        List<ReviewPlan> reviewPlanList = reviewPlanService.selectReviewPlanList(reviewPlan);
        //2.根据今日需复习的内容  查询内容具体内容
        List<HashMap> studyContentList = new ArrayList<>();
        for (int i = 0; i < reviewPlanList.size(); i++) {
            StudyContent studyContent = studyContentService.selectStudyContentById(reviewPlanList.get(i).getStudyContentId());
            HashMap hashMap = JSON.parseObject(JSON.toJSONString(studyContent), HashMap.class);
            hashMap.put("reviewStatus",reviewPlanList.get(i).getReviewStatus());
            hashMap.put("rid",reviewPlanList.get(i).getId());
            studyContentList.add(hashMap);
        }
        return studyContentList;
    }
}
